package com.sailfish.springbootdemo.dao.db1;

import com.sailfish.springbootdemo.common.DB1TableName;
import com.sailfish.springbootdemo.pojo.db1.BaseCapacityRule;
import com.sailfish.springbootdemo.pojo.db1.LineBaseCapacityValue;
import com.sailfish.springbootdemo.pojo.db1.LineTechCapacityValue;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.type.JdbcType;

import java.util.List;

public interface LineTechCapacityValueDao {

    /**
     * 根据线体id查询
     *
     * @param pcbaLineId
     * @return
     */
    @Results(id = "lineTechCapacityValueResultMap", value = {
            @Result(column = "line_tech_capacity_value_id", property = "lineTechCapacityValueId", jdbcType = JdbcType.INTEGER),
            @Result(column = "line_tech_capacity_param_key", property = "lineTechCapacityParamKey", jdbcType = JdbcType.VARCHAR),
            @Result(column = "line_tech_capacity_param_name", property = "lineTechCapacityParamName", jdbcType = JdbcType.VARCHAR),
            @Result(column = "pcba_line_id", property = "pcbaLineId", jdbcType = JdbcType.INTEGER),
            @Result(column = "line_tech_capacity_param_value", property = "lineTechCapacityParamValue", jdbcType = JdbcType.VARCHAR),
            @Result(column = "line_tech_capacity_param_value_actual", property = "lineTechCapacityParamValueActual", jdbcType = JdbcType.VARCHAR),
            @Result(column = "line_tech_capacity_param_value_actual_audit", property = "lineTechCapacityParamValueActualAudit", jdbcType = JdbcType.VARCHAR),
            @Result(column = "create_time", property = "createTime", jdbcType = JdbcType.DATETIMEOFFSET),
            @Result(column = "update_time", property = "updateTime", jdbcType = JdbcType.DATETIMEOFFSET),
    })
    @Select("select * from " + DB1TableName.LINE_TECH_CAPACITY_VALUE + " where pcba_line_id = #{pcbaLineId}")
    List<LineTechCapacityValue> getAllLineTechCapacityValueByLineId(@Param("pcbaLineId") Integer pcbaLineId);

    /**
     * 根据线体id + key 查询
     *
     * @param pcbaLineId
     * @param lineTechCapacityParamKey
     * @return
     */
    @ResultMap(value = "lineTechCapacityValueResultMap")
    @Select("select * from " + DB1TableName.LINE_TECH_CAPACITY_VALUE + " where pcba_line_id = #{pcbaLineId} and line_tech_capacity_param_key = #{lineTechCapacityParamKey}")
    LineTechCapacityValue getLineTechCapacityValueByKeyAndLineId(@Param("pcbaLineId") Integer pcbaLineId, @Param("lineTechCapacityParamKey") String lineTechCapacityParamKey);

    /**
     * 根据线体id 删除
     *
     * @param pcbaLineId
     * @return
     */
    @Delete("delete from " + DB1TableName.LINE_TECH_CAPACITY_VALUE + " where pcba_line_id = #{pcbaLineId}")
    Integer deleteLineTechCapacityValueByLineId(@Param("pcbaLineId") Integer pcbaLineId);

    /**
     * 插入
     *
     * @param lineTechCapacityValue
     * @return
     */
    @Insert("insert into " + DB1TableName.LINE_TECH_CAPACITY_VALUE + "(line_tech_capacity_param_key,line_tech_capacity_param_name,pcba_line_id,line_tech_capacity_param_value,create_time,line_tech_capacity_param_value_actual_audit) values " +
            "(#{lineTechCapacityValue.lineTechCapacityParamKey},#{lineTechCapacityValue.lineTechCapacityParamName},#{lineTechCapacityValue.pcbaLineId},#{lineTechCapacityValue.lineTechCapacityParamValue},#{lineTechCapacityValue.createTime},#{lineTechCapacityValue.lineTechCapacityParamValueActualAudit})")
    Integer addLineTechCapacityValue(@Param("lineTechCapacityValue") LineTechCapacityValue lineTechCapacityValue);

    /**
     * 更新
     *
     * @param lineTechCapacityValue
     * @return
     */
    @Update("<script>" +
            "update " + DB1TableName.LINE_TECH_CAPACITY_VALUE +
            "<set>" +
            "<if test='lineTechCapacityValue.lineTechCapacityParamValueActual != null '>" +
            " line_tech_capacity_param_value_actual = #{lineTechCapacityValue.lineTechCapacityParamValueActual}," +
            "</if>" +
            "<if test='lineTechCapacityValue.lineTechCapacityParamValueActualAudit != null '>" +
            " line_tech_capacity_param_value_actual_audit = #{lineTechCapacityValue.lineTechCapacityParamValueActualAudit}," +
            "</if>" +
            "<if test='lineTechCapacityValue.lineTechCapacityParamValue != null and lineTechCapacityValue.lineTechCapacityParamValue != \"\" '>" +
            " line_tech_capacity_param_value = #{lineTechCapacityValue.lineTechCapacityParamValue}," +
            "</if>" +
            "<if test='lineTechCapacityValue.pcbaLineId != null and lineTechCapacityValue.pcbaLineId != \"\" '>" +
            " pcba_line_id = #{lineTechCapacityValue.pcbaLineId}," +
            "</if>" +
            "<if test='lineTechCapacityValue.lineTechCapacityParamName != null and lineTechCapacityValue.lineTechCapacityParamName != \"\" '>" +
            " line_tech_capacity_param_name = #{lineTechCapacityValue.lineTechCapacityParamName}," +
            "</if>" +
            "<if test='lineTechCapacityValue.lineTechCapacityParamKey != null and lineTechCapacityValue.lineTechCapacityParamKey != \"\" '>" +
            " line_tech_capacity_param_key = #{lineTechCapacityValue.lineTechCapacityParamKey}," +
            "</if>" +
            "<if test='lineTechCapacityValue.updateTime != null '>" +
            " update_time = #{lineTechCapacityValue.updateTime}," +
            "</if>" +
            "</set>" +
            " where line_tech_capacity_value_id = #{lineTechCapacityValue.lineTechCapacityValueId}" +
            "</script>")
    Integer updateLineTechCapacityValue(@Param("lineTechCapacityValue") LineTechCapacityValue lineTechCapacityValue);

    /**
     * 清空对应的临时值（审核通过）
     */
    @Update("update " + DB1TableName.LINE_TECH_CAPACITY_VALUE + " set line_tech_capacity_param_value_actual_audit = '' where pcba_line_id = #{pcbaLineId}")
    Integer clearValueAudit(@Param("pcbaLineId") Integer pcbaLineId);

    /**
     * 将 value 还原成 ValueAudit 的值（审核不通过）
     */
    @Update("update " + DB1TableName.LINE_TECH_CAPACITY_VALUE + " set line_tech_capacity_param_value_actual = line_tech_capacity_param_value_actual_audit where pcba_line_id = #{pcbaLineId}")
    Integer returnValueFromValueAudit(@Param("pcbaLineId") Integer pcbaLineId);

    /**
     * 将当前值记录到审核字段中
     */
    @Update("update " + DB1TableName.LINE_TECH_CAPACITY_VALUE + " set line_tech_capacity_param_value_actual_audit = line_tech_capacity_param_value_actual where pcba_line_id = #{pcbaLineId}")
    Integer returnValueAuditFromValue(@Param("pcbaLineId") Integer pcbaLineId);
}
